<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="信息描述">
        <el-tag type="info">excel模版说明</el-tag>
        <el-tag>
          <i class="el-icon-download" />
          <a :href="defaultExcelTemplate">点击下载模版</a>
        </el-tag>

      </el-form-item>

      <el-form-item label="选择Excel">
        <el-upload ref="upload" :auto-upload="false" :on-exceed="fileUploadExceed" :on-success="fileUploadSuccess"
          :on-error="fileUploadError" :limit="1" :action="actionUrl" name="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <el-button :disabled="importBtnDisabled" style="margin-left: 10px;" size="small" type="success" @click="submitUpload()">导入</el-button>
        </el-upload>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        defaultExcelTemplate: 'https://vcourse-file-1.oss-cn-hangzhou.aliyuncs.com/excel/课程分类列表模板.xlsx',
        importBtnDisabled: false,
        actionUrl: process.env.VUE_APP_BASE_API + '/admin/edu/subject/import'
      }
    },

    methods: {
      // 执行上传
      submitUpload() {
        this.importBtnDisabled = true
        // 手动提交
        this.$refs.upload.submit()
      },
      fileUploadExceed() {
        this.$message.warning(`当前限制只能选择 1 个文件`);
      },
      // 上传成功的回调
      fileUploadSuccess(response) {
        console.log(response)
        if (response.success) {
          this.importBtnDisabled = false // 启用按钮
          this.$message.success(response.message)
          this.$refs.upload.clearFiles() // 清空文件列表
        } else {
          this.$message.error('上传失败! （非20000）')
        }
      },
      // 上传失败的回调
      fileUploadError() {
        this.importBtnDisabled = false // 启用按钮
        this.$message.error('上传失败! （http失败）')
        this.$refs.upload.clearFiles() // 清空文件列表
      }
    }
  }
</script>

<style>
</style>
